import requests
import time
import execjs
import json

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
    'X-Application': 'Pdfreader.Web',
    # 'X-Nonce': '47108d8c-37be-3959-5ab1-8f6366d0bca4',
    # 'X-Sign': 'DC2DBA0E5258318C32A283F90FEA26DE',
    'X-Timestamp': f'{int(time.time())}',
    'X-Token': 'null',
    'X-Version': 'V2.2',
}

json_data = {
    "UniversityCode": "",
    "MajorCode": "",
    "PageIndex": 1,
    "PageSize": 30,
}


# 执行JS脚本，生成nonce和sign
js_code = open('request_params_decrypt.js', 'r', encoding='utf-8').read()
ctx = execjs.compile(js_code)
# 必须要用json.dumps方法把python字段变成json字符串，不能自己加引号
# 这种样子传入body是不行的 '{"UniversityCode":"","MajorCode":"","PageIndex":1,"PageSize":30}'
body = json.dumps(json_data)  # 重点
nonce, sign = ctx.call("create_all_params", body)
print(nonce, sign)
headers.update({
    'X-Nonce': nonce,
    'X-Sign': sign,
})
print(headers)
response = requests.post('https://ky.douding.cn//Api/Web/GetDocumentInfos', headers=headers, json=json_data)
print(response.text)
